package com.skyable.device.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.skyable.device.entity.DomainDeviceInfo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author GaoYuan
 * @title: DomainDeviceInfoMapper
 * @projectName private-cloud
 * @description: TODO
 * @date 2021/7/316:01
 */
@Mapper
public interface DomainDeviceInfoMapper extends BaseMapper<DomainDeviceInfo> {

    /**
     * 创建设备表
     * @param domainId
     * @return
     */
    @Update({
            " CREATE TABLE IF NOT EXISTS ${domainId}_device_info ( " +
                    " physical_device_id varchar(64) NOT NULL DEFAULT '' COMMENT '设备物理ID', " +
                    " sub_domain_id bigint(20) DEFAULT NULL COMMENT '子域ID', " +
                    " name varchar(32) NOT NULL DEFAULT '' COMMENT '设备名称', " +
                    " aes_key varchar(255) NOT NULL DEFAULT '' COMMENT '密钥', " +
                    " status tinyint(4) NOT NULL DEFAULT '1' COMMENT ' 0(INVALID), 1(ACTIVE), 2(FROZEN), 3(DELETED), 4(PRE_BIND)', " +
                    " reserve varchar(64) NOT NULL DEFAULT '', " +
                    " parent_id varchar(64) DEFAULT NULL COMMENT '父设备ID', " +
                    " create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', " +
                    " modify_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后修改时间', " +
                    " PRIMARY KEY (`physical_device_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;"
    })
    int createDeviceInfoTable(@Param("domainId") Long domainId);

    /**
     * 批量插入设备信息
     * @param deviceInfoList
     * @param domainId
     * @return
     */
    @Insert({
            "<script> " +
                    "INSERT INTO ${domainId}_device_info(physical_device_id,sub_domain_id,name,aes_key,status,reserve,parent_id,create_time,modify_time) VALUES " +
                    "<foreach collection='deviceInfoList' item='item' index='index' separator=','> " +
                    "(#{item.physicalDeviceId},#{item.subDomainId},#{item.name},#{item.aesKey},#{item.status},#{item.reserve},#{item.parentId},#{item.createTime},#{item.modifyTime}) " +
                    "</foreach>" +
                    "</script> "
    })
    int saveBatchDomainDevice(@Param("deviceInfoList") List<DomainDeviceInfo> deviceInfoList, @Param("domainId") Long domainId);
}
